package com.xcfw.dorm.controller.dormassistant;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xcfw.dorm.controller.BaseController;
import com.xcfw.dorm.entity.ExaminePlan;
import com.xcfw.dorm.entity.ExamineStaff;
import com.xcfw.dorm.entity.NumEnum;
import com.xcfw.dorm.entity.ReturnJson;
import com.xcfw.dorm.exception.ServiceException;
import com.xcfw.dorm.exception.ValidationException;
import com.xcfw.dorm.mapper.ExaminePlanMapper;
import com.xcfw.dorm.mapper.ExamineStaffMapper;
import com.xcfw.dorm.util.DormUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;

/**
 * @program: Dorm
 * @description: 查寝派兵
 * @author: Bruse Queen
 * @create: 2018-05-08 11:37
 **/
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/dormAssistant")
public class ExaminePlanController extends BaseController {
    @Autowired
    private ExaminePlanMapper examinePlanMapper;
    @Autowired
    private ExamineStaffMapper examineStaffMapper;

    @RequestMapping(value = "/addExaminePlan", method = RequestMethod.POST)
    public ReturnJson addExaminePlan(@RequestBody ExaminePlan examinePlan) {
        try {
            if (1 != examinePlanMapper.insertSelective(examinePlan)) {
                throw new ServiceException("新增失败！");
            }
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "每天查寝组员新增成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("ExaminePlanController---addExaminePlan--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/deleteExaminePlan", method = RequestMethod.POST)
    public ReturnJson deleteExaminePlan(@RequestBody ExaminePlan examinePlan) {
        try {
            if (1 != examinePlanMapper.deleteByPrimaryKey(examinePlan.getId())) {
                throw new ServiceException("删除失败!");
            }
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "每天查寝组员删除成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("ExaminePlanController---deleteExaminePlan--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/updateExaminePlan", method = RequestMethod.POST)
    public ReturnJson updateExaminePlan(@RequestBody ExaminePlan examinePlan) {
        try {
            if (1 != examinePlanMapper.updateByPrimaryKeySelective(examinePlan)) {
                throw new ServiceException("修改失败！");
            }
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "每天查寝组员修改成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("ExaminePlanController---updateExaminePlan--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/selectExaminePlan", method = RequestMethod.POST)
    public ReturnJson selectExaminePlan(@RequestBody ExaminePlan examinePlan) {
        try {
            if ("".equals(examinePlan.getExamineDate())) {
                examinePlan.setExamineDate(null);
            }
            if (examinePlan.getPageNum() == null || examinePlan.getPageSize() == null) {
                List<ExaminePlan> list = examinePlanMapper.selectActionSql(DormUtil.setNull(examinePlan.getId()),
                        examinePlan.getExamineDate());
                if (list.size() == 0) {
                    throw new ServiceException("暂无相关数据！");
                }
                result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "请假信息查询成功！", list, list.size());
            } else {
                PageHelper.startPage(examinePlan.getPageNum(), examinePlan.getPageSize());
                Page<ExaminePlan> list = (Page<ExaminePlan>) examinePlanMapper.selectActionSql(DormUtil.setNull(examinePlan.getId()),
                        examinePlan.getExamineDate());
                if (list.size() == 0) {
                    throw new ServiceException("暂无相关数据！");
                }
                result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "请假信息查询成功！", list, list.getTotal());
            }
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("ExaminePlanController---selectExaminePlan--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/autoExaminePlan", method = RequestMethod.POST)
    public ReturnJson autoExaminePlan(@RequestBody ExaminePlan examinePlan) {
        try {
            List<ExamineStaff> list = examineStaffMapper.selectActionSql(String.valueOf(examinePlan.getId()));
            if (list.size() == 0) {
                throw new ServiceException("无查寝组合数据");
            }

            JSONObject examine = JSON.parseObject(list.get(0).getExamine().toString());
            JSONObject dutyRoom = JSON.parseObject(list.get(0).getDutyroom().toString());

            Integer date = DormUtil.getInteget(examine.getString("date"));
            int year = Integer.parseInt(String.valueOf(date).substring(0, 4));
            int month = Integer.parseInt(String.valueOf(date).substring(4, 6));
            int day = Integer.parseInt(String.valueOf(date).substring(6, 8));

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month - 1); //7月
            int maxDate = cal.getActualMaximum(Calendar.DATE);
            System.out.println(maxDate);

            int examinSize = examine.size() - 1;
            int examineNum = 1;

            String[] dutyRoomArray = dutyRoom.getString("duty").split(",");
            int dutySize = dutyRoomArray.length;
            int dutyNum = 0;

            for (int i = day; i < maxDate; i++) {
                if (examineNum > examinSize) {
                    examineNum = 1;
                } else if (dutyNum >= dutySize) {
                    dutyNum = 0;
                } else {
                    String human = examine.getString(String.valueOf(examineNum)) + "," + dutyRoomArray[dutyNum];
                    String newDate = year + "-" + month + "-" + i;
                    ExaminePlan examinePlan1 = new ExaminePlan();
                    examinePlan1.setExamineDate(newDate);
                    examinePlan1.setExamineHuman(human);
                    examinePlanMapper.insertSelective(examinePlan1);
                }
                examineNum++;
                dutyNum++;
            }

            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "生成本月查寝人员成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("ExaminePlanController---autoExaminePlan--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }
}
